#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class Circuits {
public:
	int grid[51][51];
	int cost[51];
	int stops[51];
	int N;
	int howLong(vector <string>, vector <string>);
	void dfs(int x){
		if(cost[x] >=0) return ;
		cost[x] = 0;
		stops[x] = 0;
		for(int i=0; i<N; i++){
			if(grid[x][i] >= 0){
				dfs(i);
				cost[x] = max(cost[x], cost[i] + grid[x][i]);
			}
		}
	}
};

int Circuits::howLong(vector <string> con, vector <string> costs) {
	N = con.size();
	memset(grid, -1, sizeof(grid));
	memset(cost, -1, sizeof(cost));
	for(int i=0; i<N; i++){
		stringstream s1(con[i]), s2(costs[i]);
		int id, c;
		while(s1 >> id && s2 >> c){
			grid[i][id] = c;
		}
	}
	int ret = 0;
	int stop = 0;
	for(int i=0; i<N; i++){
		if(cost[i] < 0){
			dfs(i);
				ret = max(ret, cost[i]);
		}
		cout << cost[i] << ", ";
	}
	cout << endl;
	return ret;
}


double test0() {
	string t0[] = {"1 2",
 "2",
 ""};
	vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
	string t1[] = {"5 3",
 "7",
 ""};
	vector <string> p1(t1, t1+sizeof(t1)/sizeof(string));
	Circuits * obj = new Circuits();
	clock_t start = clock();
	int my_answer = obj->howLong(p0, p1);
	clock_t end = clock();
	delete obj;
	cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
	int p2 = 12;
	cout <<"Desired answer: " <<endl;
	cout <<"\t" << p2 <<endl;
	cout <<"Your answer: " <<endl;
	cout <<"\t" << my_answer <<endl;
	if (p2 != my_answer) {
		cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
		return -1;
	}
	else {
		cout <<"Match :-)" <<endl <<endl;
		return (double)(end-start)/CLOCKS_PER_SEC;
	}
}
double test1() {
	string t0[] = {"1 2 3 4 5","2 3 4 5","3 4 5","4 5","5",""};
	vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
	string t1[] = {"2 2 2 2 2","2 2 2 2","2 2 2","2 2","2",""};
	vector <string> p1(t1, t1+sizeof(t1)/sizeof(string));
	Circuits * obj = new Circuits();
	clock_t start = clock();
	int my_answer = obj->howLong(p0, p1);
	clock_t end = clock();
	delete obj;
	cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
	int p2 = 10;
	cout <<"Desired answer: " <<endl;
	cout <<"\t" << p2 <<endl;
	cout <<"Your answer: " <<endl;
	cout <<"\t" << my_answer <<endl;
	if (p2 != my_answer) {
		cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
		return -1;
	}
	else {
		cout <<"Match :-)" <<endl <<endl;
		return (double)(end-start)/CLOCKS_PER_SEC;
	}
}
double test2() {
	string t0[] = {"1","2","3","","5","6","7",""};
	vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
	string t1[] = {"2","2","2","","3","3","3",""};
	vector <string> p1(t1, t1+sizeof(t1)/sizeof(string));
	Circuits * obj = new Circuits();
	clock_t start = clock();
	int my_answer = obj->howLong(p0, p1);
	clock_t end = clock();
	delete obj;
	cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
	int p2 = 9;
	cout <<"Desired answer: " <<endl;
	cout <<"\t" << p2 <<endl;
	cout <<"Your answer: " <<endl;
	cout <<"\t" << my_answer <<endl;
	if (p2 != my_answer) {
		cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
		return -1;
	}
	else {
		cout <<"Match :-)" <<endl <<endl;
		return (double)(end-start)/CLOCKS_PER_SEC;
	}
}
double test3() {
	string t0[] = {"","2 3 5","4 5","5 6","7","7 8","8 9","10",
 "10 11 12","11","12","12",""};
	vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
	string t1[] = {"","3 2 9","2 4","6 9","3","1 2","1 2","5",
 "5 6 9","2","5","3",""};
	vector <string> p1(t1, t1+sizeof(t1)/sizeof(string));
	Circuits * obj = new Circuits();
	clock_t start = clock();
	int my_answer = obj->howLong(p0, p1);
	clock_t end = clock();
	delete obj;
	cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
	int p2 = 22;
	cout <<"Desired answer: " <<endl;
	cout <<"\t" << p2 <<endl;
	cout <<"Your answer: " <<endl;
	cout <<"\t" << my_answer <<endl;
	if (p2 != my_answer) {
		cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
		return -1;
	}
	else {
		cout <<"Match :-)" <<endl <<endl;
		return (double)(end-start)/CLOCKS_PER_SEC;
	}
}
double test4() {
	string t0[] = {"","2 3","3 4 5","4 6","5 6","7","5 7",""};
	vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
	string t1[] = {"","30 50","19 6 40","12 10","35 23","8","11 20",""};
	vector <string> p1(t1, t1+sizeof(t1)/sizeof(string));
	Circuits * obj = new Circuits();
	clock_t start = clock();
	int my_answer = obj->howLong(p0, p1);
	clock_t end = clock();
	delete obj;
	cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
	int p2 = 105;
	cout <<"Desired answer: " <<endl;
	cout <<"\t" << p2 <<endl;
	cout <<"Your answer: " <<endl;
	cout <<"\t" << my_answer <<endl;
	if (p2 != my_answer) {
		cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
		return -1;
	}
	else {
		cout <<"Match :-)" <<endl <<endl;
		return (double)(end-start)/CLOCKS_PER_SEC;
	}
}

int main() {
	int time;
	bool errors = false;
	
	time = test0();
	if (time < 0)
		errors = true;
	
	time = test1();
	if (time < 0)
		errors = true;
	
	time = test2();
	if (time < 0)
		errors = true;
	
	time = test3();
	if (time < 0)
		errors = true;
	
	time = test4();
	if (time < 0)
		errors = true;
	
	if (!errors)
		cout <<"You're a stud (at least on the example cases)!" <<endl;
	else
		cout <<"Some of the test cases had errors." <<endl;
}

//Powered by [KawigiEdit] 2.0!
